package com.huluobo.a2202mvvm_im_douyindemo.login;

import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.SPUtils;
import com.huluobo.a2202mvvm_im_douyindemo.IMApp;
import com.huluobo.a2202mvvm_im_douyindemo.MainActivity;
import com.huluobo.a2202mvvm_im_douyindemo.R;
import com.huluobo.a2202mvvm_im_douyindemo.bean.LoginBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.bean.Money;
import com.huluobo.a2202mvvm_im_douyindemo.databinding.ActivityLoginBinding;
import com.huluobo.a2202mvvm_im_douyindemo.db.AppDBUtils;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseMVVMActivity;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.net.Const;
import com.huluobo.a2202mvvm_im_douyindemo.register.RegisterActivity;

public class LoginActivity extends BaseMVVMActivity<ActivityLoginBinding, LoginViewModel> {

    @Override
    protected void initData() {
        viewModel.loginEaseSuccess.observe(this, loginUserBean -> {
            viewModel.login(loginUserBean.getUsername(), loginUserBean.getPassword());

        });

        viewModel.loginEaseFailed.observe(this, s -> {
            dismissProgressDialog();
            Log.i("TagA", "登录环信失败:" + s);
        });

        viewModel.loginSuccess.observe(this, loginBeanResult -> {
            dismissProgressDialog();
            Toast.makeText(this, "欢迎回来:" + loginBeanResult.getData().getUsername(), Toast.LENGTH_SHORT).show();
            SPUtils.getInstance().put(Const.PARAM_TOKEN, loginBeanResult.getData().getToken());
            //使用Room数据库保存用户
            AppDBUtils.getInstance().getUserDao().insertUserInfo(loginBeanResult.getData());
            AppDBUtils.getInstance().getMoneyDao().insertMoney(new Money(loginBeanResult.getData().getUsername(),
                    10000f));
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        });

        viewModel.loginFailed.observe(this, s -> {
            dismissProgressDialog();
            Log.i("TagA", "登录失败:" + s);
        });
    }

    @Override
    protected void intiView() {
        dataBinding.loginHeader.headerTitleTv.setText("登录");

        //触发登录
        dataBinding.loginToLoginBtn.setOnClickListener(v -> {
            if (dataBinding.loginUsernameEt.getText().toString().matches(IMApp.REGEX_USERNAME)) {
                //匹配用户名正则,验证密码是否匹配
                if (dataBinding.loginPasswordEt.getText().toString().matches(IMApp.REGEX_PASSWORD)) {
                    viewModel.loginEase(
                            dataBinding.loginUsernameEt.getText().toString(),
                            dataBinding.loginPasswordEt.getText().toString()
                    );
                    showProgressDialog("正在登录...");
                } else {
                    dataBinding.loginPasswordEt.setError("密码不匹配,需要3-20位数字");
                }
            } else {
                dataBinding.loginUsernameEt.setError("用户名不匹配,需要3-20位字符,首字符为英文");
            }
        });

        dataBinding.loginToRegisterTv.setOnClickListener(v -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        });
    }

    @Override
    protected Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }
}